mui.init();
var old_back = mui.back;
mui.back = function(){
    var indexPage = plus.webview.getLaunchWebview();
    plus.storage.setItem(KeyCfg.currClickTabId,"userAccount");
    indexPage.show();
    mui.fire(indexPage,'refresh',{});
}

var detail = "detail_";	/*折让率文本框ID前缀*/

var btnIds = [];/*赎回按钮ID数组*/

mui.plusReady(function(){

	loadDatas();
	loadDatas2();
});

mui(".hongbao-select").on("tap", "div", function() {
	var self = this;
	$("#TypeSelected").removeAttr("id").removeClass("activity");
	$(self).attr("id", "TypeSelected").addClass("activity");
});

//未使用的红包	
function loadDatas(){
	var url = UrlConfig.hongbao_getDate;
	// 弹出系统等待对话框   
	//var w = mui.plus.nativeUI.showWaiting( "正在加载..." );
	var formDatas = {isavailable:0};
	MuiEventMgr.ajax(url,{pars:formDatas,
	  	sfn : function(data){
	  		callback(data);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  		//mui.plus.nativeUI.closeWaiting();
	  	}
	 });
}

function callback(data){
	var cashDetailList = data.cashDetailList;
	if(!cashDetailList || cashDetailList.length == 0){
		$(".cmw_noinvest").show();
		$(".mui-loading").hide();
		return;	
	}
	
	var htmlArr = [];
	for(var i=0,count=cashDetailList.length; i<count; i++){
		var cashDetail = cashDetailList[i];
		var html = getHtml(cashDetail);
		htmlArr[i] = html;
	}
	var htmlCodes = htmlArr.join("  ");
	$("#item1_loanRecords").html(htmlCodes);
	addBtnListeners();

}

//已使用或已失效的红包	
function loadDatas2(){
	var url = UrlConfig.hongbao_getDate;
	// 弹出系统等待对话框   
	//var w = mui.plus.nativeUI.showWaiting( "正在加载..." );
	var formDatas = {isavailable:1};
	MuiEventMgr.ajax(url,{pars:formDatas,
	  	sfn : function(data){
	  		callback2(data);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  		//mui.plus.nativeUI.closeWaiting();
	  	}
	 });
}

function callback2(data){
	var cashDetailList = data.cashDetailList;
	if(!cashDetailList || cashDetailList.length == 0){
		$(".cmw_noinvest").show();
		$(".mui-loading").hide();
		return;	
	}
	
	var htmlArr = [];
	for(var i=0,count=cashDetailList.length; i<count; i++){
		var cashDetail = cashDetailList[i];
		var html = getHtml2(cashDetail);
		htmlArr[i] = html;
	}
	var htmlCodes = htmlArr.join("  ");
	$("#item2_loanRecords").html(htmlCodes);
	addBtnListeners();

}


function addBtnListeners(){	
	if(!btnIds || btnIds.length == 0) return;
	for(var i=0,count=btnIds.length; i<count; i++){
		var btnId = btnIds[i];
		var ele_btn = CmwMcore.$(btnId);
		MeventMgr.onTouchstart(ele_btn,function(event,self){
			setData2Win(self.id);
		});
	}
}

function setData2Win(btnId){
	var id =btnId.replace(detail,"");
	var pars = {id:id};
	var url = UrlConfig.hongbaoDetail;
	MuiEventMgr.ajax(url,{pars:pars,
	  	sfn : function(data){
	  		setVals(data);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  	}
	 });
}

/**
 * 为赎回页面赋值 
 * @param {Object} data
 */
function setVals(data){
	var time = data.time;
	var money = data.money;
	var use = data.use;
	if(time==null)$("#time").html("");
	$("#time").html(time);
	if(money==null)$("#money").html("");
	$("#money").html(money);
	if(typeof(use)=='undefined'||use==null){
		$("#use").html("");
	}else{		
		$("#use").html("购买标号："+use);	
	}
}

//未使用红包样式加载
function getHtml(cashDetail){
	var issueTime =cashDetail.issueTime;
	var money =cashDetail.money;
	var originalMoney = cashDetail.originalMoney;
	var type = cashDetail.type;
	var validStartTime=cashDetail.validStartTime;
	var validEndTime =cashDetail.validEndTime;
	var useStatus =cashDetail.useStatus;
	var givingNature =cashDetail.givingNature;
	var guid = cashDetail.guid;
	var btnId=detail+guid;
	btnIds[btnIds.length]=btnId;
	var htmls = [	
//		'<li class="mui-table-view-cell">',
//		'		<div>',
//		'			<div class="cmw_list_row">',
//		'				<div><span class="cell_label">赠送时间：</span>'+issueTime+'</div>',
//		'				<div><span class="cell_label">(剩/总)金额：</span>'+money+'/'+originalMoney+'</div>',
//		'			</div>',
//		'			<div class="cmw_list_row">',
//		'				<div><span class="cell_label">使用次数：</span>仅限一次</div>',
//		'				<div><span class="cell_label">类型：</span>'+type+'</div>',
//		'			</div>',
//		'			<div><span class="cell_label">有效期：</span>'+validStartTime+'&nbsp;-&nbsp;'+validEndTime+'</div>',
//		'			<div>',
//		'				<span class="cell_label">状态/明细：</span>'+useStatus+'/',
//		'				<span class="cell_label"><a id="'+btnId+'"  href="#Popover_0" class="mui-icon mui-icon-info" style="padding: 6px 12px;"></a></span>',
//		'			</div>',
//		'			<div><span class="cell_label">备注：</span>'+givingNature+'</div>',
//		'		</div>',
//		'	</li>'
		
		'			<li class="mui-table-view-cell">',
		'				<div class = "hongbao-box border-orange background-orange">',
        '                   <div class ="hbleft">',
        '                   	 <span class="useStatus">'+useStatus+'</span>',
        '                   </div>',
        '                   <div class ="hbmid">',
        '                   	 <div class="givingNature">'+givingNature+'</div>',
        '                   	 <div class="money">￥ '+money+'</div>',
        '                   	 <div class="checkTime">发放时间:'+issueTime+'</div>',
        '                   </div>',
        '                   <div class ="hbright border-orange">',
        '                   	 <div class="validTime color-orange">有效期:</div>',
        '                   	 <div class="validTime1 color-orange">'+validStartTime+'</div>',
        '                   	 <div class="validTime1 color-orange">至</div>',
        '                   	 <div class="validTime1 color-orange">'+validEndTime+'</div>',
        '                   </div>',
		'				</div>',
		'			</li>'
		
	];

	return htmls.join(" ");
}

//已使用或已失效红包样式加载
function getHtml2(cashDetail){
	var issueTime =cashDetail.issueTime;
	var money =cashDetail.money;
	var originalMoney = cashDetail.originalMoney;
	var type = cashDetail.type;
	var validStartTime=cashDetail.validStartTime;
	var validEndTime =cashDetail.validEndTime;
	var useStatus =cashDetail.useStatus;
	var givingNature =cashDetail.givingNature;
	var guid = cashDetail.guid;
	var btnId=detail+guid;
	btnIds[btnIds.length]=btnId;
	var htmls = [
		'			<li class="mui-table-view-cell">',
		'				<div class = "hongbao-box border-grey background-grey">',
        '                   <div class ="hbleft">',
        '                   	 <span class="useStatus">'+useStatus+'</span>',
        '                   </div>',
        '                   <div class ="hbmid">',
        '                   	 <div class="givingNature">'+givingNature+'</div>',
        '                   	 <div class="money">￥ '+originalMoney+'</div>',
        '                   	 <div class="checkTime">发放时间:'+issueTime+'</div>',
        '                   </div>',
        '                   <div class ="hbright border-grey">',
        '                   	 <div class="validTime color-grey">有效期:</div>',
        '                   	 <div class="validTime1 color-grey">'+validStartTime+'</div>',
        '                   	 <div class="validTime1 color-grey">至</div>',
        '                   	 <div class="validTime1 color-grey">'+validEndTime+'</div>',
        '                   </div>',
		'				</div>',
		'			</li>'
		
	];

	return htmls.join(" ");
}

